package com.activititest.other;


import com.activititest.appentity.Response;
import com.activititest.entity.HazardousChemicals;
import com.activititest.entity.RequestParams;
import com.activititest.service.IHazardousChemicalsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mfx
 * @since 2020-07-03
 */
@RestController
@RequestMapping("/dangermaterial")
public class HazardousChemicalsController {
    @Autowired
    private IHazardousChemicalsService iHazardousChemicalsService;

    @PostMapping("/getname/")
    public Response<List<HazardousChemicals>> getALLDangerMaterial(@RequestParam("current") int current, @RequestBody RequestParams requestParams) {
        QueryWrapper<HazardousChemicals> queryWrapper = new QueryWrapper<HazardousChemicals>();
        queryWrapper.select("id", "product_name")
                .and(requestParams.getProductName() != null, i -> i.like("product_name", requestParams.getProductName())
                        .or().like("cas_number", requestParams.getProductName())
                        .or().like("english_name", requestParams.getProductName()))
//                .like(requestParams.getProductName() != null, "product_name", requestParams.getProductName())
//                .like(requestParams.getAlias() != null, "alias", requestParams.getAlias())
//                .like(requestParams.getCasNumber() != null, "cas_number", requestParams.getCasNumber())
//                .like(requestParams.getRemarks() != null, "remarks", requestParams.getRemarks())
//                .like(requestParams.getEnglishName() != null, "english_name", requestParams.getEnglishName())
//                .like(requestParams.getUnNumber() != null, "un_number", requestParams.getUnNumber())
//                .like(requestParams.getNumberOfRegistrations() != null, "number_of_registrations", requestParams.getNumberOfRegistrations())
//                .like(requestParams.getUpdateTime() != null, "update_time", requestParams.getUpdateTime())
                .like(requestParams.getHazardCategory1() != null, "hazard_category", requestParams.getHazardCategory1())
                .like(requestParams.getHazardCategory2() != null, "hazard_category", requestParams.getHazardCategory2())
                .like(requestParams.getHazardCategory3() != null, "hazard_category", requestParams.getHazardCategory3());
//                .like(requestParams.getGhsWarning() != null, "ghs_warning", requestParams.getGhsWarning())
//                .like(requestParams.getHazardStatement() != null, "hazard_statement", requestParams.getHazardStatement())
//                .like(requestParams.getPhysicalChemicalProperties() != null, "physical_chemical_properties", requestParams.getPhysicalChemicalProperties())
//                .like(requestParams.getMainPurpose() != null, "main_purpose", requestParams.getMainPurpose())
//                .like(requestParams.getRiskCombustionExplosion() != null, "risk_combustion_explosion", requestParams.getRiskCombustionExplosion())
//                .like(requestParams.getActiveReaction() != null, "active_reaction", requestParams.getActiveReaction())
//                .like(requestParams.getTaboos() != null, "taboos", requestParams.getTaboos())
//                .like(requestParams.getInvasiveUse() != null, "invasive_use", requestParams.getInvasiveUse())
//                .like(requestParams.getToxicity() != null, "toxicity", requestParams.getToxicity())
//                .like(requestParams.getPoisoningManifestations() != null, "poisoning_manifestations", requestParams.getPoisoningManifestations())
//                .like(requestParams.getOccupationalExposureLimit() != null, "occupational_exposure_limit", requestParams.getOccupationalExposureLimit())
//                .like(requestParams.getEnvironmentalHazards() != null, "environmental_hazards", requestParams.getEnvironmentalHazards())
//                .like(requestParams.getFirstAidMeasures() != null, "first_aid_measures", requestParams.getFirstAidMeasures())
//                .like(requestParams.getLeakageEmergencyDisposal() != null, "leakage_emergency_disposal", requestParams.getLeakageEmergencyDisposal())
//                .like(requestParams.getFireFightingMethods() != null, "fire_fighting_methods", requestParams.getFireFightingMethods());
        Page<HazardousChemicals> p = new Page<>(current, 30);
        IPage<HazardousChemicals> iPage = iHazardousChemicalsService.page(p, queryWrapper);
        List<HazardousChemicals> list = iPage.getRecords();
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(list);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

    @PostMapping("/getcount/")
    public Response<Integer> getCount(@RequestBody RequestParams requestParams) {

        QueryWrapper<HazardousChemicals> queryWrapper = new QueryWrapper<HazardousChemicals>();
        queryWrapper
                .and(requestParams.getProductName() != null, i -> i.like("product_name", requestParams.getProductName())
                        .or().like("cas_number", requestParams.getProductName())
                        .or().like("english_name", requestParams.getProductName()))
//                .like(requestParams.getProductName() != null, "product_name", requestParams.getProductName())
//                .like(requestParams.getAlias() != null, "alias", requestParams.getAlias())
//                .like(requestParams.getCasNumber() != null, "cas_number", requestParams.getCasNumber())
//                .like(requestParams.getRemarks() != null, "remarks", requestParams.getRemarks())
//                .like(requestParams.getEnglishName() != null, "english_name", requestParams.getEnglishName())
//                .like(requestParams.getUnNumber() != null, "un_number", requestParams.getUnNumber())
//                .like(requestParams.getNumberOfRegistrations() != null, "number_of_registrations", requestParams.getNumberOfRegistrations())
//                .like(requestParams.getUpdateTime() != null, "update_time", requestParams.getUpdateTime())
                .like(requestParams.getHazardCategory1() != null, "hazard_category", requestParams.getHazardCategory1())
                .like(requestParams.getHazardCategory2() != null, "hazard_category", requestParams.getHazardCategory2())
                .like(requestParams.getHazardCategory3() != null, "hazard_category", requestParams.getHazardCategory3());
//                .like(requestParams.getGhsWarning() != null, "ghs_warning", requestParams.getGhsWarning())
//                .like(requestParams.getHazardStatement() != null, "hazard_statement", requestParams.getHazardStatement())
//                .like(requestParams.getPhysicalChemicalProperties() != null, "physical_chemical_properties", requestParams.getPhysicalChemicalProperties())
//                .like(requestParams.getMainPurpose() != null, "main_purpose", requestParams.getMainPurpose())
//                .like(requestParams.getRiskCombustionExplosion() != null, "risk_combustion_explosion", requestParams.getRiskCombustionExplosion())
//                .like(requestParams.getActiveReaction() != null, "active_reaction", requestParams.getActiveReaction())
//                .like(requestParams.getTaboos() != null, "taboos", requestParams.getTaboos())
//                .like(requestParams.getInvasiveUse() != null, "invasive_use", requestParams.getInvasiveUse())
//                .like(requestParams.getToxicity() != null, "toxicity", requestParams.getToxicity())
//                .like(requestParams.getPoisoningManifestations() != null, "poisoning_manifestations", requestParams.getPoisoningManifestations())
//                .like(requestParams.getOccupationalExposureLimit() != null, "occupational_exposure_limit", requestParams.getOccupationalExposureLimit())
//                .like(requestParams.getEnvironmentalHazards() != null, "environmental_hazards", requestParams.getEnvironmentalHazards())
//                .like(requestParams.getFirstAidMeasures() != null, "first_aid_measures", requestParams.getFirstAidMeasures())
//                .like(requestParams.getLeakageEmergencyDisposal() != null, "leakage_emergency_disposal", requestParams.getLeakageEmergencyDisposal())
//                .like(requestParams.getFireFightingMethods() != null, "fire_fighting_methods", requestParams.getFireFightingMethods());
        int count = iHazardousChemicalsService.count(queryWrapper);
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(count);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

    @PostMapping("/gettextview/")
    public Response<HazardousChemicals> getBasicInfo(@RequestParam("id") int id) {
        QueryWrapper<HazardousChemicals> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        HazardousChemicals hazardousChemicals = iHazardousChemicalsService.getOne(queryWrapper);
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(hazardousChemicals);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

    @Value("${spring.file.download-path}")
    private String filePathPrefix;

    @PostMapping("/downloadpictogram/")
    public Response<List<String>> getPictogram(@RequestParam("fileUrl") String fileUrl) {
        List<String> list = new ArrayList<>();
        String[] urls = fileUrl.split("/");
        Response euserResponse = new Response();
        for (int i = 0; i < urls.length; i++) {
            try {
                InputStream in = new FileInputStream(new File(filePathPrefix + urls[i] + ".jpg"));
                byte[] body = null;
                body = new byte[in.available()];
                in.read(body);
                in.close();
                String s = Base64.getEncoder().encodeToString(body);
                list.add(s);
            } catch (IOException e) {
                continue;
            }
        }
        euserResponse.setCode(200);
        euserResponse.setData(list);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

//    @PostMapping("/test/")
//    public String getTest() {
//        QueryWrapper<HazardousChemicals> queryWrapper = new QueryWrapper<HazardousChemicals>();
//        queryWrapper.select("id", "product_name");
//        List<HazardousChemicals> l = iHazardousChemicalsService.list(queryWrapper);
//        for (HazardousChemicals hazardousChemicals:l){
//            if (new File("D:/image/" + hazardousChemicals.getProductName() + ".jpg").exists()){
//                HazardousChemicals h = new HazardousChemicals();
//                h.setId(hazardousChemicals.getId());
//                h.setPictogram(hazardousChemicals.getProductName()+"/");
//                iHazardousChemicalsService.updateById(h);
//                System.out.println("-------" + hazardousChemicals.getId() + "----------");
//                continue;
//            }
//            HazardousChemicals hc = new HazardousChemicals();
//            String pi = "";
//            if (new File("D:/image/" + hazardousChemicals.getProductName() + "-1.jpg").exists()){
//                hc.setId(hazardousChemicals.getId());
//                pi += hazardousChemicals.getProductName()+"-1/";
//            }
//            if (new File("D:/image/" + hazardousChemicals.getProductName() + "-2.jpg").exists()){
//                pi += hazardousChemicals.getProductName()+"-2/";
//            }
//            if (new File("D:/image/" + hazardousChemicals.getProductName() + "-3.jpg").exists()){
//                pi += hazardousChemicals.getProductName()+"-3/";
//            }
//            if (new File("D:/image/" + hazardousChemicals.getProductName() + "-4.jpg").exists()){
//                pi += hazardousChemicals.getProductName()+"-4/";
//            }
//            if (new File("D:/image/" + hazardousChemicals.getProductName() + "-5.jpg").exists()){
//                pi += hazardousChemicals.getProductName()+"-5/";
//            }
//            if (new File("D:/image/" + hazardousChemicals.getProductName() + "-6.jpg").exists()){
//                pi += hazardousChemicals.getProductName()+"-6/";
//            }
//            hc.setPictogram(pi);
//            iHazardousChemicalsService.updateById(hc);
//        }
//        return "ok";
//    }
}

